#coding=utf-8
'''
Created on 2017-6-13

@author: sandy
'''

import logging
from pycommon import Const
from pycommon import Pack as packet
import ManagerBuild
import zlib
import os
import hashlib
import ServerConfig
import sys
import imp

def cmdKeepAlive(manager,pkt):
    pass

def cmdLoginRet(manager,pkt):
    ecode = packet.unpack("H", pkt)
    if ecode != Const.ERR_OK:
        logging.info(u'Login Manager Failed ecode=%d', ecode)
    else:
        logging.info(u'Login Manager Sucess ecode=%d', ecode)
        index = ServerConfig.instance.index
        if index == 1:
            fileDir = ServerConfig.instance.upload_dir
            upFiles = []
            for parent,dirnames,filenames in os.walk(fileDir):
                for filename in filenames:
                    path = os.path.join(parent,filename)
                    upFiles.append(path)
            for file in upFiles:
                f = open(file)
                content = f.read()
                f.close()
                strContent = zlib.compress(content)
                na = file.replace(os.sep,'.')[len(fileDir)+1:-5]
                logging.debug(u"upload config file cfgname = %s",na)
                md5str = hashlib.md5(content).hexdigest()
                manager.sendPacket(ManagerBuild.uploadConfigFile(na,md5str,strContent))
        elif index == 2:
            fileDir = ServerConfig.instance.hot_update
            hotFiles = []
            for parent,dirnames,filenames in os.walk(fileDir):
                for filename in filenames:
                    path = os.path.join(parent,filename)
                    hotFiles.append(path)
            for file in hotFiles:
                f = open(file)
                content = f.read()
                f.close()
                strContent = zlib.compress(content)
                na = file.replace(os.sep,'.')[len(fileDir)+1:]
                logging.debug(u"upload hot file cfgname = %s",na)
                md5str = hashlib.md5(content).hexdigest()
                hotServers = ServerConfig.instance.hot_servers
                hotServers = ",".join(str(s) for s in hotServers)
                manager.sendPacket(ManagerBuild.uploadHotFile(na,md5str,strContent,hotServers))
    pass


def cmdUploadRet(manager,pkt):
    cfgname,upRet = packet.unpack("ss",pkt)
    logging.debug(u"upload result:%s %s",cfgname,upRet)

def cmdHotUpdateFile(manager,pkt):
    cfg,codeStr = packet.unpack("ss",pkt)
    logging.debug(u"hotUpdate result:%s %s",cfg,codeStr)

__cmdTable = {
              Const.CMD_KEEP_ALIVE             : cmdKeepAlive,
              Const.CMD_MGR2XX_LOGIN_RET       : cmdLoginRet,
              Const.CMD_MGR2XX_UPLOAD_FILE_RET : cmdUploadRet,
              Const.CMD_MGR2XX_HOT_UPDATE_FILE : cmdHotUpdateFile
             }
def parse(manager,cmd,pkt):
    if cmd == Const.CMD_KEEP_ALIVE:
        pass
    logging.debug(u"ManagerParse cmd = %d",cmd)
    func = __cmdTable.get(cmd)
    if not func :
        logging.debug(u"unknow cmd=%d" % cmd)
        return
    func(manager, pkt)